package july.android.cakes.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import july.android.cakes.service.STeacherClass;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author S_Cake
 * @version 教师端消息通知
 */
@RestController
@EnableAutoConfiguration
public class CTeacherClass {

    @Autowired
    private STeacherClass sv;

    //根据“教师号”获取其所教所有班级
    @RequestMapping("/get_myClass")
    public List<Map<String, Object>> do_getMyClass(@RequestParam("tea_id") String tea_id) {
        return sv.getMyClass(tea_id);
    }

    //根据“课程号、专业号、年级”获取组消息
    @RequestMapping("/get_myClassMessage")
    public List<Map<String, Object>> do_getMyClassMessage(@RequestParam("c_id") String c_id, @RequestParam("m_id") String m_id, @RequestParam("grade") String grade) {
        return sv.getMyClassMessage(c_id, m_id, grade);
    }

    //根据传入的教师工号获取其加入的讨论组
    @RequestMapping("/get_teacherSinDiscussion")
    public List<Map<String, Object>> do_getTeacherSinDiscussion(@RequestParam("tea_id") String tea_id) {
        return sv.getTeacherSinDiscussion(tea_id);
    }

    //根据“讨论组号”获取讨论组消息
    @RequestMapping("/get_myDisMessage")
    public List<Map<String, Object>> do_getMyDisMessage(@RequestParam("id") String id) {
        return sv.getMyDisMessage(id);
    }

    //把“讨论组号，发送人，信息”插入dis_chat
    @RequestMapping("/add_DisChat")
    public void do_addDisChat(@RequestParam("g_id") String g_id, @RequestParam("id") String pull, @RequestParam("chat") String chat) {
        sv.addDisChat(g_id, pull, chat);
    }

    //根据“教师号”获取其教的全部学生
    @RequestMapping("/get_myStudent")
    public List<Map<String, Object>> do_getMyStudent(@RequestParam("tea_id") String tea_id) {
        return sv.getMyStudent(tea_id);
    }

    //获取全部教师信息
    @RequestMapping("/get_allTeacher")
    public List<Map<String, Object>> do_AllTeacher() {
        return sv.allTeacher();
    }

    //获取建讨论组时所需信息
    @RequestMapping("/get_groupNeed")
    public List<Map<String, Object>> do_GroupNeed(@RequestParam("tea_id") String tea_id) {
        return sv.groupNeed(tea_id);
    }

    //创建讨论组
    @RequestMapping("/create_dis")
    public void do_createDis(@RequestParam("data[]") String[] data, HttpServletResponse hp) throws IOException {
        //hp.addHeader("Access-Control-Allow-Origin","*");
         sv.createDis(data);

        //hp.sendRedirect("http://123.206.198.158/back.html");
        //hp.sendRedirect("http://127.0.0.1/back.html");
    }

    //根据“教师号”获取某个教师上传的全部课件
    @RequestMapping("/get_all_files")
    public List<Map<String,Object>>do_getallfiles(@RequestParam("tea_id")String tea_id){
        return sv.getallfiles(tea_id);
    }
    
    //根据“课件号”获取授权的年级专业
    @RequestMapping("/get_file_master")
    public List<Map<String,Object>>do_getFileMaster(@RequestParam("f_id")String f_id){
        return sv.getFileMaster(f_id);
    }
    
    //根据“课件号”将课件删除
    @RequestMapping("/del_a_file")
    public void do_delAfile(@RequestParam("f_id")int f_id){
        sv.delAfile(f_id);
    }
    
   
    /**
     * **********************************************************************************************
     */
    //向数据库插入上传的课件的详细信息
    @RequestMapping("/add_files")
    public void do_addfiles(@RequestParam("nname") String nname, @RequestParam("url") String url, @RequestParam("tea_id") String tea_id, @RequestParam("major") String major, @RequestParam("grade") String grade) throws UnsupportedEncodingException {
        sv.addfiles(nname, url, tea_id, major, grade);
    }

     //根据教师号，专业名，年级获取某个教师上传的课件信息
    @RequestMapping("/get_sin_files")
    public List<Map<String,Object>>do_getsinfiles(@RequestParam("tea_id")String tea_id,@RequestParam("major_id")String major_id,@RequestParam("grade")String grade){
        return sv.getsinfiles(tea_id,major_id,grade);
    }
    
 
    //根据传入的教师工号及其针对某个专业发布的课程消息内容插入数据库
    @RequestMapping("/addteachercoursemessage")
    public void do_addteachercoursemessage(@RequestParam("id") String id, @RequestParam("grade") String grade, @RequestParam("m_id") String m_id, @RequestParam("c_id") String c_id, @RequestParam("chat") String chat) throws UnsupportedEncodingException {
        sv.addteachercoursemessage(id, grade, m_id, c_id, chat);
    }

    //根据传入的教师工号及其讨论组发布的消息内容插入数据库，前台应保存讨论组号
    @RequestMapping("/addteachergroupmessage")
    public void do_addteachergroupmessage(@RequestParam("tea_id") String tea_id, @RequestParam("dis_id") String dis_id, @RequestParam("chat") String chat) throws UnsupportedEncodingException {
        sv.addteachergroupmessage(tea_id, dis_id, chat);
    }

    //若前台没有保存讨论组号，可通过以下方法获得讨论组号
    @RequestMapping("/get_dis_id")
    public String do_getdis_id(@RequestParam("dis_name") String dis_name) {
        return sv.getdis_id(dis_name);
    }

    //根据传来的教师工号获得其所教的年级专业（方法一）
    @RequestMapping("/getgrademajor")
    public List<Map<String, Object>> do_getgrademajor(@RequestParam("id") String id) {
        return sv.getgrade_major(id);
    }

    //根据传来的教师工号获得其所教的年级专业（方法二）
    @RequestMapping("/getgrademajor2")
    public List<Map<String, Object>> do_getgrademajor2(@RequestParam("id") String id) {
        return sv.getgrade_major2(id);
    }

    //获得根据学号排列的学生信息
    @RequestMapping("/get_idsort")
    public List<Map<String, Object>> do_getidsort(@RequestParam("grade") String grade, @RequestParam("major") String major) {
        return sv.getidsort(grade, major);
    }

    //获得根据姓氏排列的学生信息
    @RequestMapping("/get_namesort")
    public List<Map<String, Object>> do_getnamesort(@RequestParam("grade") String grade, @RequestParam("major") String major) {
        return sv.getnamesort(grade, major);
    }

    //根据传入的教师工号获取其所教年级专业及通知消息
    @RequestMapping("/get_postmessage")
    public List<Map<String, Object>> do_getpostmessage(@RequestParam("tea_id") String tea_id) {
        return sv.getpostmessage(tea_id);
    }

    //根据传入的教师工号判断其是否加入讨论组，若加入则显示讨论组名字及讨论组消息
    @RequestMapping("/get_teacherdiscussion")
    public List<Map<String, Object>> do_getteacherdiscussion(@RequestParam("tea_id") String tea_id) {
        return sv.getteacherdiscussion(tea_id);
    }

    //插入讨论组名字（必须是数据库中没有的，否则提醒用户该讨论组名字已存在），并返回讨论组的dis_id，前台应以变量存之
    @RequestMapping("/add_discussionname")
    public String do_adddiscussionname(@RequestParam("name") String name) {
        return sv.adddiscussionname(name);
    }

    //根据传入的教师工号和讨论组的id往数据库表dis2tea中插入数据
    @RequestMapping("/add_dis2tea")
    public void do_adddis_tea(@RequestParam("dis_id") String dis_id, @RequestParam("tea_id") String tea_id) {
        sv.adddis_tea(dis_id, tea_id);
    }

    //根据传入的学生学号和讨论组的id往数据库表dis2stu中插入数据
    @RequestMapping("/add_dis2stu")
    public void do_adddis_stu(@RequestParam("dis_id") String dis_id, @RequestParam("stu_id") String stu_id) {
        sv.adddis_stu(dis_id, stu_id);
    }

    //根据学生学号进行搜索
    @RequestMapping("/searchbyid")
    public List<Map<String, Object>> do_searchbyid(@RequestParam("stu_id") String stu_id) {
        return sv.searchbyid(stu_id);
    }

    //根据学生姓名进行搜索
    @RequestMapping("/searchbyname")
    public List<Map<String, Object>> do_searchbyname(@RequestParam("stu_name") String stu_name) {
        return sv.searchbyid(stu_name);
    }

    //根据教师工号进行搜索
    @RequestMapping("/searchid")
    public List<Map<String, Object>> do_searchid(@RequestParam("tea_id") String tea_id) {
        return sv.searchid(tea_id);
    }

    //根据教师姓名进行搜索
    @RequestMapping("/searchname")
    public List<Map<String, Object>> do_searchname(@RequestParam("tea_name") String tea_name) {
        return sv.searchname(tea_name);
    }

}
